<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="useracc">
                    <el-input v-model="param.useracc" placeholder="请输入账号">
                        <template #prepend>
                            <el-button :icon="User"></el-button>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="param.password" @keyup.enter="submitForm(login)">
                        <template #prepend>
                            <el-button :icon="Lock"></el-button>
                        </template>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm(login)">登录</el-button>
                </div>
                <div class="register-btn">
                    <el-button type="warning" @click="jumpToRegister">去注册</el-button>
                </div>
                <p class="login-tips">Tips : 登录界面。</p>
            </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">
// 导入Vue3相关的API
import { ref, reactive } from "vue";
// 数据管理
import { useTagsStore } from "../store/tags";
import { usePermissStore } from "../store/permiss";
import { useLoginStore } from "../store/user";
// 路由导航API
import { useRouter } from "vue-router";
// elementplus 弹窗
import { ElMessage } from "element-plus";
// elementplus组件
import type { FormInstance, FormRules } from "element-plus";
// 图标
import { Lock, User } from "@element-plus/icons-vue";
// 导入登录接口
import { loginFunc } from "../api/index";

// 接口（规范表单的数据）
interface LoginInfo {
    useracc: string;
    password: string;
}
// 创建路由实例（跳转）
const router = useRouter();
// 创建响应式对象
const param = reactive<LoginInfo>({
    useracc: "",
    password: "",
});
// 给表单做正则表达式（不能为空之类的提示）
const rules: FormRules = {
    useracc: [
        {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
        },
    ],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};
const permiss = usePermissStore();

// 创建表单响应式对象
const login = ref<FormInstance>();
const loginUser = useLoginStore();

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.validate((valid: boolean) => {
        console.log("xxx:", valid);
        if (valid) {
            // ElMessage.success('登录成功');
            // localStorage.setItem('ms_useracc', param.useracc);
            // router.push('/');
            // 登录接口
            let admin_acc = param.useracc;
            let admin_pwd = param.password;
            loginFunc({ user_acc: admin_acc, user_pwd: admin_pwd })
                .then((content) => {
                    // console.log(content)
                    // 解构赋值
                    let { code, msg, token, res } = content;
                    console.log(res[0]);

                    if (code == 200) {
                        ElMessage({
                            message: msg,
                            type: "success",
                        });
                        loginUser.setLogin(res[0]);
                        // const keys = permiss.defaultList[param.useracc == 'admin' ? 'admin' : 'user'];
                        // permiss.handleSet(keys);
                        // localStorage.setItem('ms_keys', JSON.stringify(keys));

                        // 缓存数据
                        localStorage.setItem("ms_username", param.useracc);
                        localStorage.setItem("admin_token", token);
                        // 跳转
                        router.push("/");
                    } else {
                        ElMessage({
                            message: msg,
                            type: "warning",
                        });
                    }
                })
                .catch((err) => {
                    console.log(err);
                });
        } else {
            // ElMessage.error('登录成功');
            return false;
        }
    });
};

const tags = useTagsStore();
tags.clearTags();

// 跳转注册页
const jumpToRegister = () => {
    console.log(456);
    router.push({
        path: "/register",
    });
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn,
.register-btn {
    text-align: center;
}
.login-btn button,
.register-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>
